import myRouter from '@/router'
import { getQueryVariable, isEmpty } from '../utils/httputils'

export function loginSuccess(token: string, userName: string) {
  localStorage.setItem('token', token)
  localStorage.setItem('userName', userName)
  // console.log('保存：' + userName)
}
export function logout() {
  localStorage.clear()
  myRouter.replace('/login')
}
export function getToken() {
  let token: string = getQueryVariable('token')
  if (isEmpty(token)) {
    token = localStorage.getItem('token') as string
  }
  if (isEmpty(token)) {
    token = sessionStorage.getItem('token') as string
  }
  try {
    if (isEmpty(token)) {
      token = window.android.getToken()
    }
  } catch {}
  return token
}
export function isLogin() {
  return getToken() != null
}
export function getName() {
  // console.log('获取userName' + localStorage.getItem('userName'))
  return localStorage.getItem('userName')
}
